package chapter01_stackandqueue;

import java.util.Stack;

/**
 * 描述：
 *      用一个栈给另一个站排序
 * @author hl
 * @date 2021/1/14 11:07
 */
public class StackSort {

    public static void sortStackByStack(Stack<Integer> stack){
        Stack<Integer> helper = new Stack<>();
        int cur = 0;
        while(!stack.isEmpty()){
            cur = stack.pop();
            while(!helper.isEmpty() && helper.peek() > cur){
                stack.push(helper.pop());
            }
            helper.push(cur);
        }
        while(!helper.isEmpty()){
            stack.push(helper.pop());
        }
    }

    public static void main(String[] args) {
        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < 10; i++) {
            stack.push(i);
        }
        System.out.println(stack);
        sortStackByStack(stack);
        System.out.println(stack);
    }



}
